import 'dart:convert';

import 'package:flutter/material.dart';

import '../../../base/base_model.dart';
import '../../../resources.dart';
import '../m/note_detail_model.dart';

/// created by hcl
/// on 2025/5/5
/// HomeModel
/// desc
class HomeModel extends BaseModel {
  bool showDetail = false;
  FocusNode searchFocusNode = FocusNode();
  List<NoteDetailModel> notes = [];

  int currentIndex = -1;
  int maxTitleLength = 20;
  NoteDetailModel currentNoteDetailModel = NoteDetailModel.fromMap({});
  TextEditingController titleController = TextEditingController();
  FocusNode titleFocusNode = FocusNode();
  TextEditingController contentController = TextEditingController();
  FocusNode contentFocusNode = FocusNode();

  @override
  void initData() {
    noteList();
  }

  noteList() {
    notes.clear();
    String noteStr = GSpUtils.get(GSpUtils.noteList);
    try {
      var temp = json.decode(noteStr);
      if (temp != null && temp is List && temp.isNotEmpty) {
        for (var element in temp) {
          var detail = NoteDetailModel.fromMap(element);
          if (detail.content.isNotEmpty) {
            notes.add(detail);
          }
        }
      }
    } catch (e) {}
    notifyListeners();
  }

  void changeShowDetail(bool detail) {
    /// detail为true显示详情 需要先判断index，如果是-1代表新增，如果不是-1代表修改
    if (detail) {
      if (currentIndex > -1) {
        var itemModel = notes[currentIndex];
        String content = GSpUtils.get(itemModel.createTime);
        if (content.isNotEmpty) {
          currentNoteDetailModel = itemModel.copyWith(content: content);
        }
        titleController.text = currentNoteDetailModel.title;
        contentController.text = currentNoteDetailModel.content;
      } else {
        titleController.text = '';
        contentController.text = '';
        int tempTime = DateTime.now().millisecondsSinceEpoch;
        currentNoteDetailModel = NoteDetailModel.fromMap({})
            .copyWith(createTime: '$tempTime', updateTime: tempTime);
        contentFocusNode.requestFocus();
      }
    } else {
      String tempTitle = titleController.text;
      String tempContent = contentController.text;
      if (currentIndex > -1) {
        notes.removeAt(currentIndex);
      }

      if (tempContent.isNotEmpty) {
        currentNoteDetailModel.updateTime =
            DateTime.now().millisecondsSinceEpoch;
        currentNoteDetailModel.content = tempContent.substring(
            0,
            tempContent.length > maxTitleLength
                ? maxTitleLength
                : tempContent.length);
        currentNoteDetailModel.title =
            tempTitle.isNotEmpty ? tempTitle : currentNoteDetailModel.content;
        notes.insert(0, currentNoteDetailModel);

        GSpUtils.set(currentNoteDetailModel.createTime, tempContent);
        saveNotes();
      }
    }
    showDetail = detail;
    notifyListeners();
  }

  void remove(int index) {
    notes.removeAt(index);
    saveNotes();
    notifyListeners();
  }

  void saveNotes() {
    List savaNotes = [];
    for (var element in notes) {
      savaNotes.add(element.toMap());
    }
    GSpUtils.set(GSpUtils.noteList, json.encode(savaNotes));
  }
}
